package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginPermission;
import cn.wolfcode.wolf2w.annotation.UserInfoResolver;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.redis.service.IStrategyRedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.ui.context.Theme;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyRedisService strategyRedisService;


    /**
     * 获取单个攻略
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Object getToasts(String id){
        Strategy strategy = strategyService.get(id);

        //阅读数+1
        strategyRedisService.viewnemIncrease(id,1);


        return JsonResult.success(strategy);
    }

    /**
     * 查询目的地下所有攻略
     * @param qo
     * @return
     */
    @GetMapping("/query")
    public Object queryStrategies(StrategyQueryObject qo){
        Page page = strategyService.queryStrategiesBydestId(qo);

        return JsonResult.success(page);
    }

    /**
     * 查询所有主题
     * @return
     */
    @GetMapping("/themes")
    public Object queryThemes(){
        List<StrategyTheme> list = strategyThemeService.list();

        return JsonResult.success(list);
    }

    /**
     * 添加评论
     * @param strategyComment
     * @param userInfo
     * @return
     */
    @LoginPermission
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, @UserInfoResolver UserInfo userInfo){

        //工具类, 将用户信息赋值给评论对象
        BeanUtils.copyProperties(userInfo, strategyComment);
        //赋值给userId
        strategyComment.setUserId(userInfo.getId());
        //创建时间
        strategyComment.setCreateTime(new Date());

        //添加评论
        strategyCommentService.save(strategyComment);

        //添加评论后,评论数+1
        strategyRedisService.replynumIncrease(strategyComment.getStrategyId(),1);

        return JsonResult.success();
    }

    /**
     * 评论分页
     * @param qo
     * @return
     */
    @GetMapping("/comments")
    public Object comments(StrategyCommentQueryObject qo){
        Page page = strategyCommentService.query(qo);

        return JsonResult.success(page);
    }


    /**
     * 评论点赞
     * @param cid
     * @param userInfo
     * @return
     */
    @LoginPermission
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, @UserInfoResolver UserInfo userInfo){

        //获取用户id
        String uid = userInfo.getId();

        //点赞
        strategyCommentService.commentThumb(cid,uid);

        return JsonResult.success();
    }

    /**
     * 获取统计数据
     * @param sid
     * @return
     */
    @GetMapping("/statisVo")
    public Object statisVo(String sid){

        return JsonResult.success(strategyRedisService.getStatisVo(sid));
    }


    /**
     * 添加收藏/取消收藏
     * @param sid
     * @param user
     * @return
     */
    @LoginPermission
    @PostMapping("/favor")
    public Object favor(String sid,@UserInfoResolver UserInfo user){

        boolean rel = strategyRedisService.StrategyFavor(sid,user.getId());

        return JsonResult.success(rel);
    }

    /**
     * 点赞
     * @param sid
     * @param user
     * @return
     */
    @LoginPermission
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserInfoResolver UserInfo user){

        boolean rel = strategyRedisService.strategyThumbup(sid,user.getId());

        return JsonResult.success(rel);
    }
}
